﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
using System.IO;

namespace Ketoan
{
	/// <summary>
	/// Summary description for Excel.
	/// </summary>
	public class exportExcel
	{
		//Khai bao cac bien su dung cho class
		private Excel.Application oxl;
		private Excel._Workbook owb;
		private Excel._Worksheet osheet;
		//End khai bao bien

		public exportExcel()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		#region Dong tat ca cac ket noi va dataset khi ket thuc form
		public void Dispose()
		{
			try
			{
				quitExcel();
				GC.Collect();
			}
			catch{};
		}
		#endregion

		//public frmReport(LibKetoan.AccessData acc,DataSet ds_Data,string s_Tieude,string s_Ngay,string s_Tc,string s_GT11,string s_GT12,string s_GT21,string s_GT22,string s_GT31,string s_GT32,string s_GT41,string s_GT42,string s_GT51,string s_GT52,string sReport)
		private void quitExcel()
		{
			//owb.Close(true, null, null);
			//oxl.Quit();

			if (osheet != null)
			{
				System.Runtime.InteropServices.Marshal.ReleaseComObject(osheet);
			}
			if (owb != null)
			{
				System.Runtime.InteropServices.Marshal.ReleaseComObject(owb);
			}
			if (oxl != null)
			{
				System.Runtime.InteropServices.Marshal.ReleaseComObject(oxl);
			}

			osheet = null;
			owb = null;
			oxl = null;

			GC.Collect();
		}
		public string expExcel(LibKetoan.AccessData a,DataSet ds_Data,string s_Tieude,string s_Ngay,string s_Tc,string s_GT11,string s_GT12,string s_GT21,string s_GT22,string s_GT31,string s_GT32,string s_GT41,string s_GT42,string s_GT51,string s_GT52,string sReport)
		{
			string s_Finish="";
			try
			{
				//a.check_process_Excel();
				//string tenfile=(b_baohiem)?a.Export_Excel(dt,sReport.Substring(0,sReport.Length-4)):a.Export_Excel(ds_Data,sReport.Substring(0,sReport.Length-4));
				string tenfile=a.Export_Excel(ds_Data,sReport.Substring(0,sReport.Length-4));
				if (tenfile!="")
				{
					oxl=new Excel.Application();
					if (oxl!=null)
					{
						owb=(Excel._Workbook)(oxl.Workbooks.Open(tenfile,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value));
						osheet=(Excel._Worksheet)owb.ActiveSheet;
						oxl.ActiveWindow.DisplayGridlines=true;
						oxl.ActiveWindow.DisplayZeros=false;
						osheet.PageSetup.Orientation=XlPageOrientation.xlLandscape;
						osheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;
						osheet.PageSetup.LeftMargin=20;
						osheet.PageSetup.RightMargin=20;
						osheet.PageSetup.TopMargin=30;
						osheet.PageSetup.CenterFooter="Trang : &P/&N";
						oxl.Visible=true;
						quitExcel();
					}
					else s_Finish="Không khởi động được Excel";
				}
				else 
				{ 
					string s_Tenfile=sReport.Trim().Substring(0,sReport.Length-4)+".xls";
					s_Finish="File Excel "+s_Tenfile+" đang được mở";
				}
			}
			catch
			{
				s_Finish="Lỗi xuất sang Excel";
				quitExcel();
			}
			finally
			{
				quitExcel();
			}
			return s_Finish;
		}
	}
}
